package Game.entity;

import Game.functions.Hitable;
import Game.functions.Recyclable;
import Game.utils.CollisionUtils;
import Game.utils.Direction;
import Game.utils.Meta;
import Game.utils.SoundUtils;

import java.io.IOException;
import java.util.Random;

public class EnemyTank extends Element implements Hitable, Recyclable {
    int num = 5;
    int surplus = 0;
    //游戏启动后坦克默认的指向
    Direction d = Direction.RIGHT;

    Direction unMoved;

    public EnemyTank(int x, int y) {
        super(x, y);
        super.path = "15-Tankwar/images/enemy_1_r.gif";
        getSize();
        super.hp = 1;
    }

    public EnemyTank() {
    }

    public void move(Direction d) {

        if (unMoved == d && unMoved != null) {
//          停止前走完留白距离
            switch (d) {
                case UP:
                    y -= surplus;
                    break;

                case DOWN:
                    y += surplus;
                    break;

                case LEFT:
                    x -= surplus;
                    break;

                case RIGHT:
                    x += surplus;
                    break;
            }
            return;
        }
        if (d == this.d) {
            switch (d) {
                case UP:
                    y -= num;
                    break;
                case DOWN:
                    y += num;
                    break;
                case LEFT:
                    x -= num;
                    break;
                case RIGHT:
                    x += num;
                    break;
            }
        } else {
//          转向不要修改坐标！！！，被坑惨了
            switch (d) {
                case UP:
                    path = "15-Tankwar\\images\\enemy_1_u.gif";
//                    y -= direction;
                    break;
                case DOWN:
                    path = "15-Tankwar\\images\\enemy_1_d.gif";
//                    y += direction;
                    break;
                case LEFT:
                    path = "15-Tankwar\\images\\enemy_1_l.gif";
//                    x -= direction;
                    break;
                case RIGHT:
                    path = "15-Tankwar\\images\\enemy_1_r.gif";
//                    x += direction;
                    break;
            }
            this.d = d;

        }

//      防止越界
        if (x < 0) x = 0;

        if (x > Meta.WIDTH - this.width) {
            x = Meta.WIDTH - this.width;
        }
        if (y < 0) y = 0;

        if (y > Meta.HEIGHT - this.height) {
            y = Meta.HEIGHT - this.height;
        }
    }

    long gap = 1000;
    long lastTime;

    public Bullet fire() {

        long currentTime = System.currentTimeMillis();
        if (currentTime - lastTime >= gap) {
            lastTime = currentTime;
            try {
                SoundUtils.play("15-Tankwar\\audio\\fire.wav");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return new Bullet(x, y, width, height, d);
        } else {
            return null;
        }
    }

    public boolean isCollide(Element e) {
        //获取坦克的宽高坐标
        int x1 = this.x;
        int y1 = this.y;
        int w1 = this.width;
        int h1 = this.height;

        switch (d) {
            case UP:
                y1 -= num;
                break;

            case DOWN:
                y1 += num;
                break;

            case LEFT:
                x1 -= num;
                break;

            case RIGHT:
                x1 += num;
                break;
        }

        //获取墙体的宽高坐标
        int x2 = e.x;
        int y2 = e.y;
        int w2 = e.width;
        int h2 = e.height;

        boolean flag = CollisionUtils.isCollisionWithRect(x1, y1, w1, h1, x2, y2, w2, h2);

        if (flag) {
            unMoved = d;

            switch (d) {
                case UP:
                    surplus = super.y - y2 - h2;
                    break;
                case DOWN:
                    surplus = y2 - super.y - h1;
                    break;
                case LEFT:
                    surplus = super.x - x2 - w2;
                    break;
                case RIGHT:
                    surplus = x2 - super.x - w1;
                    break;
            }

        }else {
            unMoved = null;
        }
        return flag;
    }

    //使用随机数生成器随机一个值
    private final Random r = new Random();
    long e_interval = 2500;
    long e_lastRandomTime;

    @Override
    public  void draw(){
        super.draw();
        //获取当前系统时间
        long currentRandomTime = System.currentTimeMillis();
        //判断此次时间-上一次生成随机数的时间是否满足间隔
        if(currentRandomTime - e_lastRandomTime >= e_interval){
            //生成随机数
            int num = r.nextInt(4);
            //根据随机数设置方向
           switch (num) {
               case 0:
                   this.move(Direction.UP);
                   break;
               case 1:
                   this.move(Direction.DOWN);
                   break;
               case 2:
                   this.move(Direction.LEFT);
                   break;
               case 3:
                   this.move(Direction.RIGHT);
                   break;
           }
            //记录下当前时间
            e_lastRandomTime = currentRandomTime;
        }else {
            this.move(d);
        }
    }

    @Override
    public Blast showBlast() {
        hp--;
        return new Blast(x, y, width, height);
    }

    @Override
    public boolean isOutside() {
        return hp <= 0;
    }
}

